	clear all
	set more off

	
	use "$data/secondary/final_data.dta", clear
	
	merge 1:1 ID using "$data/secondary/disaggregated_mistakes.dta", keep(match) nogen
	
	merge 1:1 ID using "$data/choice_data.dta", keep(match) keepusing(risk_*) nogen
	
	merge 1:1 ID using "$data/secondary/Houtman and Maks/HM_toys.dta", keep(master match) nogen
	
	merge 1:1 ID using "$data/secondary/Houtman and Maks/HM_sharing.dta", keep(master match) nogen
	
	merge 1:1 ID using "$data/secondary/Houtman and Maks/HM_risk.dta", keep(master match) nogen
	
	*********************************************************************
	***	CONTINUOUS MEASURE OF VIOLATIONS OF MONOTONICITY IN RISK TASK ***
	*********************************************************************
	 
	forvalues i = 1/8 {
		gen risk_monotonicity`i' = .
	} 
	 
	*** riskless is dominated (3 vs 7.5) SD = 9	
		replace risk_monotonicity1 = 0				if  risk_G_K == 5 
		replace risk_monotonicity1 = -(7.5 - 3)		if 	risk_G_K == 1 
		replace risk_monotonicity1 = -(7.5 - 3)/2	if	risk_G_K == 15
		
	*** riskless is dominated (3 vs 4.5) SD = 3
		replace risk_monotonicity2 = 0				if  risk_G_M == 7 
		replace risk_monotonicity2 = -(4.5 - 3)		if 	risk_G_M == 1 
		replace risk_monotonicity2 = -(4.5 - 3)/2	if	risk_G_M == 15
		
	*** risky is dominated (3 vs 1.5) SD = 3	
		replace risk_monotonicity3 = 0				if  risk_G_N == 1 
		replace risk_monotonicity3 = -(3 - 1.5)		if 	risk_G_N == 8 
		replace risk_monotonicity3 = -(3 - 1.5)/2	if	risk_G_N == 15
	
	*** risky is dominated (6 vs 4.5) (SD = 3)	
		replace risk_monotonicity4 = 0				if  risk_H_M == 2 
		replace risk_monotonicity4 = -(6 - 4.5)		if 	risk_H_M == 7 
		replace risk_monotonicity4 = -(6 - 4.5)/2	if	risk_H_M == 15
		
	*** risky is dominated (6 vs 1.5) (SD = 3)
		replace risk_monotonicity5 = 0				if  risk_H_N == 2 
		replace risk_monotonicity5 = -(6 - 1.5)		if 	risk_H_N == 8 
		replace risk_monotonicity5 = -(6 - 1.5)/2	if	risk_H_N == 15
		
	*** risky is dominated (9 vs 4.5) (SD = 9)
		replace risk_monotonicity6 = 0				if  risk_I_L == 3 
		replace risk_monotonicity6 = -(9 - 4.5)		if 	risk_I_L == 6 
		replace risk_monotonicity6 = -(9 - 4.5)/2	if	risk_I_L == 15
		
	*** risky is dominated (9 vs 4.5) (SD = 3)	
		replace risk_monotonicity7 = 0				if  risk_I_M == 3 
		replace risk_monotonicity7 = -(9 - 4.5)		if 	risk_I_M == 7 
		replace risk_monotonicity7 = -(9 - 4.5)/2	if	risk_I_M == 15
		
	*** risky is dominated (9 vs 1.5) (SD = 3)
		replace risk_monotonicity8 = 0				if  risk_I_N == 3  	
		replace risk_monotonicity8 = -(9 - 1.5)		if 	risk_I_N == 8 
		replace risk_monotonicity8 = -(9 - 1.5)/2	if	risk_I_N == 15
		
	 egen risk_monotonicity = rowtotal(risk_monotonicity*)
		
	
	
	**********************************************************************
	***	USING HM instead of TOTAL NUMBER of VIOLATIONS of TRANSITIVITY ***
	**********************************************************************
		
	gsem (hm_toys hm_sharing hm_risk risk_dominated <- X, ologit)

	predict DMQ_alt1, latent	
	
	*****************************************************************************
	***	USING AMOUNT LEFT ON THE TABLE instead of NUMBER OF DOMINATED CHOICES ***
	*****************************************************************************

	gsem (toys_transitivity sharing_transitivity risk_consistency risk_monotonicity <- X)

	predict DMQ_alt2, latent	
	
	replace DMQ_alt2 = -DMQ_alt2
		
	**********************************************************************
	***					USING ANDERSON (2008) INDEX 	   			   ***
	**********************************************************************
	
	capture prog drop gen_index
	program gen_index, rclass

		version 13
		syntax varlist(numeric min=1), Generate(name)

		confirm new variable `generate'

		local thecount : word count `varlist'
		qui corr `varlist' if ITT == 0, cov
		matrix covmat=r(C)
		matrix theones=J(`thecount',1,1)
		matrix theweight=invsym(covmat)*theones

		qui gen `generate'=0
		local thecount=1
		foreach thevar of var `varlist' {
					  qui replace `generate'=`generate'+`thevar'*theweight[`thecount',1]
					  local thecount=`thecount'+1
		}

		qui {
		
			summ `generate' if ITT == 0
			replace `generate' = (`generate' - r(mean))/r(sd)
			cap drop u
			
		}

	end
	*/	
	
	foreach var in toys_transitivity sharing_transitivity risk_consistency risk_dominated {
		
		qui summ `var' if ITT == 0
		gen st_`var' = (`var' - r(mean))/r(sd)
		
	}
	
	gen_index st_toys_transitivity st_sharing_transitivity st_risk_consistency st_risk_dominated, g(DMQ_alt4)              

	replace DMQ_alt4 = -DMQ_alt4

	**********************************************************************
	***				USING PRINCIPAL COMPONENT ANALYSIS 	   			   ***
	**********************************************************************

    *** PRINCIPAL COMPONENT ANALYSIS
	
	pca toys_transitivity sharing_transitivity risk_consistency risk_dominated
	predict DMQ_alt3
	
	replace DMQ_alt3 = -DMQ_alt3

	
	**********************************************************************
	***			USING DISAGGREGATED MEASURES of IMPERFECT CHOICES 	   ***
	**********************************************************************
	
	gsem (toys_transitivity1 toys_transitivity2 toys_transitivity3 toys_transitivity4 toys_transitivity5 toys_transitivity6 toys_transitivity7 toys_transitivity8 toys_transitivity9 toys_transitivity10 toys_transitivity11 toys_transitivity12 toys_transitivity13 toys_transitivity14 toys_transitivity15 toys_transitivity16 toys_transitivity17 toys_transitivity18 toys_transitivity19 toys_transitivity20 sharing_transitivity1 sharing_transitivity2 sharing_transitivity3 sharing_transitivity4 sharing_transitivity5 sharing_transitivity6 sharing_transitivity7 sharing_transitivity8 sharing_transitivity9 sharing_transitivity10 risk_consistency2 risk_consistency3 risk_consistency4 risk_consistency5 risk_consistency6 risk_consistency7 risk_consistency8 risk_dominated1 risk_dominated2 risk_dominated3 risk_dominated4 risk_dominated5 risk_dominated6 risk_dominated7 risk_dominated8 <- X, logit) // , ologit

	predict DMQ_alt5, latent	
	
	replace DMQ_alt5 = -DMQ_alt5


	
	xtset cluster
		
	
	xtreg st_DMQ male_ITT female_ITT male, r fe
	
	forvalues i = 1/5 {
		
		summ DMQ_alt`i' if ITT == 0
		
		gen st_DMQ_alt`i' = (DMQ_alt`i' - r(mean))/r(sd)
		
		xtreg st_DMQ_alt`i' male_ITT female_ITT male, r fe
		
	}
